package hu.bme.mit.Riflab.Drools;

import hu.bme.mit.rifl.main.OrderWF;

import java.util.Collection;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseConfiguration;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.conf.EventProcessingOption;
import org.drools.definition.KnowledgePackage;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.KnowledgeSessionConfiguration;
import org.drools.runtime.StatefulKnowledgeSession;


public class MainDrools {

    private KnowledgeBuilder kbuilder;
    private Collection<KnowledgePackage> pkgs;
    private KnowledgeBase kbase;
    private StatefulKnowledgeSession ksession;
    //private WorkingMemoryEntryPoint entryPoint1;
    private KnowledgeRuntimeLogger logger;
	
	public static void main (String[] args){
		MainDrools drools = new MainDrools();
		drools.initDrools();

		for (int i = 0; i < 600; i++)
		{
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		drools.logger.close();
		drools.ksession.dispose();
		
	}	
	
    private void initDrools() {
    	kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("poc.drl", MainDrools.class), ResourceType.DRL);

        if (kbuilder.hasErrors()) {
            System.out.println(kbuilder.getErrors().toString());
            throw new RuntimeException("Unable to compile drl\".");
        }
        pkgs = kbuilder.getKnowledgePackages();

        KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
        config.setOption(EventProcessingOption.STREAM);		//STREAM mode option
        kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase = KnowledgeBaseFactory.newKnowledgeBase(config);		//set KnowledgeBase into STREAM mode

        kbase.addKnowledgePackages(pkgs);

        KnowledgeSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();

        ksession = kbase.newStatefulKnowledgeSession();	//New Stateful Session
        logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "log/logger");

        OrderWF o = new OrderWF();
        o.start(ksession);
    }
  
}
